package com.mkz.admin.controller;


import javax.validation.Valid;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkz.admin.entity.SysDict;
import com.mkz.admin.entity.SysDictItem;
import com.mkz.admin.service.SysDictItemService;
import com.mkz.admin.service.SysDictService;
import com.mkz.config.feign.ParamClient;
import com.mkz.core.common.constant.CacheConstants;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.log.annotation.SysLog;
import com.mkz.job.entity.JobInfoVo;
import com.mkz.job.feign.JobinfoClient;
import com.mkz.producer.feign.ProducerClient;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author XXXXXX
 * @since 2019-03-19
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
@Api(value = "dict", tags = "字典管理模块")
public class DictController {
	private final SysDictItemService sysDictItemService;
	private final SysDictService sysDictService;
	private final ParamClient paramClient;
	private final ProducerClient producerClient;
	private final JobinfoClient jobinfoClient;

	/**
	 * 通过ID查询字典信息
	 *
	 * @param id ID
	 * @return 字典信息
	 */
	@GetMapping("/{id}")
	public ResponseResult getById(@PathVariable Integer id) {
		return ResponseResult.ok(sysDictService.getById(id));
	}

	/**
	 * 分页查询字典信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@SuppressWarnings("rawtypes")
	@GetMapping("/page")
	public ResponseResult getDictPage(Page page, SysDict sysDict) {
//		log.info("testparam======================="+paramClient.get("testabc"));
//		producerClient.basicSend("my_test", "ddddd", "222");
//		JobInfoVo jobInfoVo = new JobInfoVo("0 22 11 * * ?", "demoJobHandler", "动态测试0923", "mkz-excutor-test");
//		jobinfoClient.save(jobInfoVo);
		return ResponseResult.ok(sysDictService.page(page, Wrappers.query(sysDict)));
	}

	/**
	 * 通过字典类型查找字典
	 *
	 * @param type 类型
	 * @return 同类型字典
	 */
	@GetMapping("/type/{type}")
	@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type")
	public ResponseResult getDictByType(@PathVariable String type) {
		return ResponseResult.ok(sysDictItemService.list(Wrappers
			.<SysDictItem>query().lambda()
			.eq(SysDictItem::getType, type)));
	}

	/**
	 * 添加字典
	 *
	 * @param sysDict 字典信息
	 * @return success、false
	 */
	@SysLog("添加字典")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dict_add')")
	public ResponseResult save(@Valid @RequestBody SysDict sysDict) {
		return ResponseResult.ok(sysDictService.save(sysDict));
	}

	/**
	 * 删除字典，并且清除字典缓存
	 *
	 * @param id ID
	 * @return ResponseResult
	 */
	@SysLog("删除字典")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dict_del')")
	public ResponseResult removeById(@PathVariable Integer id) {
		return sysDictService.removeDict(id);
	}

	/**
	 * 修改字典
	 *
	 * @param sysDict 字典信息
	 * @return success/false
	 */
	@PutMapping
	@SysLog("修改字典")
	@PreAuthorize("@pms.hasPermission('sys_dict_edit')")
	public ResponseResult updateById(@Valid @RequestBody SysDict sysDict) {
		return sysDictService.updateDict(sysDict);
	}

	/**
	 * 分页查询
	 *
	 * @param page        分页对象
	 * @param sysDictItem 字典项
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@GetMapping("/item/page")
	public ResponseResult getSysDictItemPage(Page page, SysDictItem sysDictItem) {
		return ResponseResult.ok(sysDictItemService.page(page, Wrappers.query(sysDictItem)));
	}


	/**
	 * 通过id查询字典项
	 *
	 * @param id id
	 * @return ResponseResult
	 */
	@GetMapping("/item/{id}")
	public ResponseResult getDictItemById(@PathVariable("id") Integer id) {
		return ResponseResult.ok(sysDictItemService.getById(id));
	}

	/**
	 * 新增字典项
	 *
	 * @param sysDictItem 字典项
	 * @return ResponseResult
	 */
	@SysLog("新增字典项")
	@PostMapping("/item")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public ResponseResult save(@RequestBody SysDictItem sysDictItem) {
		return ResponseResult.ok(sysDictItemService.save(sysDictItem));
	}

	/**
	 * 修改字典项
	 *
	 * @param sysDictItem 字典项
	 * @return ResponseResult
	 */
	@SysLog("修改字典项")
	@PutMapping("/item")
	public ResponseResult updateById(@RequestBody SysDictItem sysDictItem) {
		return sysDictItemService.updateDictItem(sysDictItem);
	}

	/**
	 * 通过id删除字典项
	 *
	 * @param id id
	 * @return ResponseResult
	 */
	@SysLog("删除字典项")
	@DeleteMapping("/item/{id}")
	public ResponseResult removeDictItemById(@PathVariable Integer id) {
		return sysDictItemService.removeDictItem(id);
	}
}
